﻿using System;
using easyccms.BusinessLayer;
using easyccms.BusinessLayer.Entities;
using easyccms.BusinessLayer.Repositories;
using easyccms.WebUI.Infrastructure;

namespace easyccms.WebUI.Code
{
    public class UserProvider
    {
        private readonly IUnitOfWork _unitOfWork;

        public UserProvider()
        {
            _unitOfWork = _userRepository.GetUnitOfWork();
        }

        private ISiteUserRepository _userRepository
        {
            get { return DependencyInjector.Resolve<ISiteUserRepository>(); }
        }

        /// <summary>
        /// Gets the current user.
        /// </summary>
        /// <returns>Static user 1</returns>
        public SiteUser GetCurrentUser()
        {
            SiteUser loadedUser = _userRepository.Load(1);
            loadedUser.LastLoginDate = DateTime.Now;


            _unitOfWork.StartTransaction();
            _userRepository.Save(loadedUser);
            _unitOfWork.CommitTransaction();

            return loadedUser;
        }
    }
}